package com.jcms.cms.dao.parcel.impl;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.jcms.cms.dao.parcel.PriceDicDao;
import com.jcms.cms.entity.parcel.PriceDic;
import com.jcms.common.hibernate3.HibernateBaseDao;
@Repository
public class PriceDicDaoImpl extends HibernateBaseDao<PriceDic, Integer> implements PriceDicDao {

	@Override
	protected Class<PriceDic> getEntityClass() {
		return PriceDic.class;
	}

	public double getPriceByCompanyAndWeight(int companyId, double weight) {
		Session session = getSession();
		Query query = session.createSQLQuery("SELECT t.price FROM p_price_dic t WHERE ABS(t.weight-:w) = (SELECT MIN(ABS(weight-:w)) FROM p_price_dic)");
		query.setDouble("w", weight);
		return Double.parseDouble(query.uniqueResult().toString());
	}

}
